# -*- coding: utf-8 -*- 
"""
Module Description:
Time : 2020/12/18 17:48 
Author : Wang P
File : gift_scope_utils.py
version: 
"""
from functools import wraps

from common.service_config import get_server_id
from pyshare.bll.utils.share.share_role_information import ShareRoleInformation
from pyshare.const.enums.item import GiftItemTuple
from pyshare.const.lang.tips_info import TipsInfo
from pyshare.dal.repository_impl import CrossAlliesRoleRepository, FriendRecordRepository, \
    AllianceMemberRepository
from framework.firefly_dc import runtime_logger as log
from pyshare.framework.error_handle import GameException


def gift_scope(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        try:
            scope = "scope_"+str(kwargs.get("scope", 0))
            if not kwargs.get("rid", 0) or not kwargs.get("receiver_rid", 0) or not kwargs.get("receiver_srv_id", 0):
                raise GameException(TipsInfo.RoleKeys.dataError)
            if scope not in GiftItemTuple:
                raise GameException(TipsInfo.RoleKeys.configError)
            scope_check = getattr(gift_scope_check, scope)
            stat, info = scope_check(kwargs.get("rid", 0), kwargs.get("receiver_rid", 0), kwargs.get("receiver_srv_id", 0))
            if not stat:
                raise GameException(TipsInfo.RoleKeys.configError)
            return func(*args, **kwargs)
        except Exception as e:
            log.error(str(e))
            return func(*args, **kwargs)
    return wrapper


class GiftScopeCheck(object):

    @staticmethod
    def scope_1(rid, receiver_rid, receiver_srv_id):
        """
        校验赠送对象是否在友邦范围内
        :param rid:
        :param receiver_rid:
        :param receiver_srv_id:
        :return:
        """
        friend_record = FriendRecordRepository.get_by_rid_and_friend_id(rid, receiver_rid)
        if not friend_record:
            return False, TipsInfo.ItemKeys.CanNotGift
        return True, ""

    @staticmethod
    def scope_2(rid, receiver_rid, receiver_srv_id):
        """
        校验赠送对象是否在盟友范围内
        :param rid:
        :param receiver_rid:
        :param receiver_srv_id:
        :return:
        """
        alliance_record = AllianceMemberRepository.filter_by_rid_list([rid, receiver_rid])
        if len(alliance_record) < 2:
            return False, TipsInfo.ItemKeys.CanNotGift
        if alliance_record[0].alliance_id != alliance_record[0].alliance_id:
            return False, TipsInfo.ItemKeys.CanNotGift
        return True, ""

    def scope_3(self, rid, receiver_rid, receiver_srv_id):
        """
        校验赠送对象是否在友邦和盟友范围内
        :param rid:
        :param receiver_rid:
        :param receiver_srv_id:
        :return:
        """
        s_f, i_f = self.scope_1(rid, receiver_rid, receiver_srv_id)
        s_a, i_a = self.scope_2(rid, receiver_rid, receiver_srv_id)
        if not s_f and not s_a:
            return False, i_f
        return True, ""

    @staticmethod
    def scope_4(rid, receiver_rid, receiver_srv_id):
        """
        校验赠送对象是否在区服范围内
        :param rid:
        :param receiver_rid:
        :param receiver_srv_id:
        :return:
        """
        role = ShareRoleInformation.get_role_information_from_cache_db(rid, get_server_id())
        if not role:
            return False, TipsInfo.RoleKeys.dataError
        if role.srv_id != receiver_srv_id:
            return False, TipsInfo.ItemKeys.NotInSameServer
        role_info = ShareRoleInformation.get_role_information_from_cache_db(receiver_rid, receiver_srv_id)
        if not role_info:
            return False, TipsInfo.RoleKeys.roleNotExist
        return True, ""

    @staticmethod
    def scope_5(rid, receiver_rid, receiver_srv_id):
        """
        校验赠送对象是否在同盟国内
        :param rid:
        :param receiver_rid:
        :param receiver_srv_id:
        :return:
        """
        role_allies = CrossAlliesRoleRepository.filter_by_rid_list([rid, receiver_rid])
        if len(role_allies) < 2:
            return False, TipsInfo.Allies.CrossAlliesNotSameInCountry
        else:
            if role_allies[0].country_id != role_allies[1].country_id:
                return False, TipsInfo.Allies.CrossAlliesNotSameInCountry
        return True, ""

    @staticmethod
    def scope_6(rid, receiver_rid, receiver_srv_id):
        """
        校验赠送对象是否在全大区内
        :param rid:
        :param receiver_rid:
        :param receiver_srv_id:
        :return:
        """
        role_info = ShareRoleInformation.get_role_information_from_cache_db(receiver_rid, receiver_srv_id)
        if not role_info:
            return False, TipsInfo.RoleKeys.roleNotExist
        return True, ""


gift_scope_check = GiftScopeCheck()


